<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-model-item label="采购物料编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseCode">
              <a-input v-model="model.purchaseCode" placeholder="请输入采购物料编码"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="物料品番" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="purchaseCode1">
              <a-input v-model="model.purchaseCode1" placeholder="请输入物料品番"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="价格编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serialNumber">
              <a-input v-model="model.serialNumber" disabled></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12" v-if="false">
            <a-form-model-item label="产品id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productId">
              <a-input v-model="model.productId" placeholder="请输入产品代码"></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="物料代码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productCode">
              <a-select
                :dropdownMatchSelectWidth="false"
                :showSearch="true"
                style="width: 100%"
                v-model:value="model.productCode"
                @search="searchMaterial"
                @blur="clearMaterialData"
                @select="changeRecord($event)"
                @popupScroll="popupScroll"
                :filter-option="false"
              >
                <a-select-option v-for="(item, index) in materialData" :key="index">
                  <span v-for="(i, index) in materialField" :key="index">
                    {{ item[i] }}
                    <a-divider type="vertical" style="background-color: #0c8fcf"></a-divider>
                  </span>
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="物料名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="productName">
              <a-input v-model="model.productName" placeholder="请输入产品名称" disabled></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sap">
              <a-input v-model="model.sap" placeholder="请输入规格型号" disabled></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit_dictText" placeholder="请输入单位" disabled></a-input>
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="供应商名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="supplierId">
              <j-dict-select-tag
                type="list"
                v-model="model.supplierId"
                dict-code="hf_supplier,name,id"
                placeholder="请输入供应商名称"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="未税单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number
                v-model="model.price"
                placeholder="请输入未税单价"
                style="width: 100%"
                :formatter="value => `￥${value}`"
                :parser="value => value.replace('￥', '')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="税率" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number
                v-model="model.tax"
                placeholder="请输入税率"
                style="width: 100%"
                :formatter="value => `${value}%`"
                :parser="value => value.replace('%', '')"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="含税单价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="taxPrice">
              <a-input-number
                v-model="model.taxPrice"
                style="width: 100%"
                disabled
                :formatter="value => `￥${value}`"
                :parser="value => value.replace('%', '')"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="执行时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="useTime">
              <j-date
                placeholder="请选择执行时间"
                v-model="model.useTime"
                style="width: 100%"
                date-format="YYYY-MM-DD HH:mm:ss"
              />
            </a-form-model-item>
          </a-col>

          <a-col :span="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" placeholder="请输入备注"></a-textarea>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { HfIODetailMixin } from '@/mixins/HfIODetailMixin'
import { HfOrderDetailMixin } from '@/mixins/HfOrderDetailMixin'

export default {
  name: 'HfPurchasePriceForm',
  mixins: [HfIODetailMixin],
  components: {},
  watch: {
    'model.price': {
      immediate: true,
      handler(newVal, oldVal) {
        this.model.taxPrice = newVal * ((this.model.tax + 100) / 100)
      }
    }
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {
        isDelete: 0,
        tax: 13
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        supplierId: [{ required: true, message: '请输入供应商!' }],
        productCode: [{ required: true, message: '请输入产品!' }],
        useTime: [{ required: true, message: '请输入执行时间!' }],
        price: [{ required: true, message: '请输入未税单价!' }],
        tax: [{ required: true, message: '请输入税率!' }]
      },
      url: {
        add: '/purchase/hfPurchasePrice/add',
        edit: '/purchase/hfPurchasePrice/edit',
        queryById: '/purchase/hfPurchasePrice/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    }
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          debugger
          httpAction(httpurl, this.model, method)
            .then(res => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    changeRecord(index) {
      //找到选中行的数据
      const target = this.materialData[index]
      //找到当前行的数据
      const current = this.model
      Object.keys(target).forEach(key => {
        if (target[key] === null) {
          target[key] = ''
        }
      })
      //设置数据
      current.productId = target.id
      current.productCode = target.materialCode
      current.productName = target.materialName
      current.sap = target.sap
      current.unit_dictText = target.unit_dictText
      current.unit = target.unit
      current.materialType_dictText = target.materialType_dictText
      current.materialType = target.materialType
      current.warehouse = target.warehouse
      current.warehouse_dictText = target.warehouse_dictText
      current.perNumber = target.perNumber
      this.selectValue = target.materialCode
      this.editingKey = ''
    }
  }
}
</script>
